package Ldzy.Servlerts.com;

import Ldzy.bean.com.User;
import Ldzy.Tabel.com.UserDB;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class LogInServler extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String name = request.getParameter("yhm");
        String pas = request.getParameter("pay");
        System.out.println(name);
        User user = UserDB.getUserByName(name);
        String msg;
        HttpSession httpSession = request.getSession();
        if (user == null) {
            msg = "用户不存在!";
            response.getWriter().write(msg);
            response.setHeader("Refresh", "3;URL=/Login.html");

        } else if (user.getPassword().equals(pas)&&user.getName().equals(name)) {
            msg = "登陆成功";
            response.getWriter().write(msg + "欢迎你，" + user.getName() + "!");
            response.getWriter().write("<a href='/outServerl'>退出登录<a/><br/>");
            response.getWriter().write("<a href='/exServler'>进入购物车选购商品<a/><br/>");
            response.getWriter().write("<a href='/pay'>进入支付<a/>");
            httpSession.setAttribute("name", name);
            Cookie cookie = new Cookie("JSESSIONID", httpSession.getId());
            response.addCookie(cookie);
        } else {
            msg = "用户名或密码不正确";
            response.getWriter().write(msg);
            response.setHeader("Refresh", "3;URL=/Login.html");

        }
    }
}
